package com.blithe.wgt.ui.activity;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Bundle;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blithe.wgt.R;
import com.blithe.wgt.core.BaseActivity;
import com.blithe.wgt.entity.Path;
import com.blithe.wgt.entity.Task;
import com.blithe.wgt.http.ProgressSubscriber;
import com.blithe.wgt.http.SubscriberOnNextListener;
import com.blithe.wgt.http.response.BaseResponse;
import com.blithe.wgt.httpService.TaskLoader;
import com.blithe.wgt.utils.DateUtils;
import com.blithe.wgt.utils.StringUtils;
import com.blithe.wgt.utils.ToastTip;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Calendar;
import java.util.Date;

public class MainActivity extends BaseActivity  {
    private Task mTask;

    private LinearLayout ll_chose_startDate,ll_chose_endDate,ll_chose_path;
    private TextView  tv_path_name,tv_start_date,tv_end_date;

    private Button btn_submit;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        ll_chose_startDate=findViewById(R.id.ll_chose_startDate);
        ll_chose_endDate=findViewById(R.id.ll_chose_endDate);
        ll_chose_path=findViewById(R.id.ll_chose_path);
        tv_path_name=findViewById(R.id.tv_path_name);
        tv_start_date=findViewById(R.id.tv_start_date);
        tv_end_date=findViewById(R.id.tv_end_date);
        btn_submit=findViewById(R.id.btn_submit);
    }

    @Override
    protected void initData() {
        mTask =new Task();
    }

    @Override
    protected void setListener() {
        ll_chose_startDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initCustomTimePicker(tv_start_date).show();
            }
        });
        ll_chose_path.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               jumpToPage(ChosePathActivity.class);
            }
        });
        ll_chose_endDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initCustomTimePicker(tv_end_date).show();
            }
        });
        btn_submit.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {

                String startDate= tv_start_date.getText().toString();
                String endDate =tv_end_date.getText().toString();

                if(mTask.getPathId()==null){
                    ToastTip.show("请选择路径");
                    return;
                }
                if(StringUtils.isEmpty(startDate)){
                    ToastTip.show("请选择开始时间");
                    return;
                }
                if(StringUtils.isEmpty(endDate)){
                    ToastTip.show("请选择结束时间");
                    return;
                }
                if(endDate.compareTo(startDate)<=0){
                    ToastTip.show("开始时间不能大于结束时间");
                    return;
                }
                mTask.setEndDate(endDate);
                mTask.setStartDate(startDate);
                new TaskLoader().postTask(mTask).subscribe(new ProgressSubscriber<String>(subscriberOnNextListener,mContext));;
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Path path) {
        MainActivity.this.runOnUiThread(new Runnable(){
            @Override
            public void run() {
                //设置选择的路线ID
                mTask.setPathId(path.getPathId());
                System.out.println(path.getPathName());
                tv_path_name.setText(path.getPathName());
            }
        });

    }



    private TimePickerView initCustomTimePicker(TextView textView) {
        /**
         * @description
         *
         * 注意事项：
         * 1.自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针.
         * 具体可参考demo 里面的两个自定义layout布局。
         * 2.因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
         * setRangDate方法控制起始终止时间(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
         */
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(2023, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2050, 2, 28);

        TimePickerView pvCustomTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                textView.setText(DateUtils.dateToString(date,"yyyy-MM-dd HH:mm:ss"));
            }
        }).setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
            @Override
            public void onTimeSelectChanged(Date date) {

            }
        })
        .setType(new boolean[]{true, true, true, true, true, true})
        .setItemVisibleCount(6)
        .setLineSpacingMultiplier(2.0f)
        .isAlphaGradient(true)
        .build();
        return pvCustomTime;
    }
    private SubscriberOnNextListener<String> subscriberOnNextListener=new SubscriberOnNextListener<String>(){

        @Override
        public void onNext(BaseResponse<String> response) {
            if(response!=null&&response.code==200){

                ToastTip.show("任务发送成功");

            }
        }

        @Override
        public void onComplete() {

        }

        @Override
        public void onError(String msg) {
            ToastTip.show("任务发送失败");
        }
    };

    @Override
    protected void onDestroy() {

        if(waitDialog!=null){
            waitDialog.dismiss();
        }
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

}
